$(function(){
	var page=1;
	var tddata=[
		{
			key:'order_no',
			type:1,
			url:''
		},{
			key:'order_type2',
			type:1,
			url:'{:url("detail")}',
			id:137
		},{
			key:'order_state2',
			type:1,
			url:''
		},{
			key:'orderer',
			type:1,
			url:''
		},{
			key:'order_time',
			type:1,
			url:''
		},{
			key:'passenger_name',
			type:1,
			url:''
		},{
			key:'passenger_phone',
			type:1,
			url:''
		},{
			key:'company_name',
			type:1,
			url:''
		},{
			key:'from_addr',
			type:1,
			url:''
		},{
			key:'to_addr',
			type:1,
			url:''
		},{
			key:'approval',
			type:1,
			url:''
		},{
			key:'driver_name',
			type:1,
			url:''
		},{
			key:'car_number',
			type:1,
			url:''
		},{
			key:'car_type_name',
			type:1,
			url:''
		},{
			key:'use_explain',
			type:1,
			url:''
		},{
			key:'passenger_money',
			type:1,
			url:''
		},{
			key:'meter',
			type:1,
			url:''
		},{
			key:'use_time',
			type:1,
			url:''
		},{
			key:'apply_time',
			type:1,
			url:''
		},{
			key:'approval_time',
			type:1,
			url:''
		},{
			key:'orders_time',
			type:1,
			url:''
		},{
			key:'ride_time',
			type:1,
			url:''
		}];
	
	var addoperation=``;
	
	var data={
		page:page,
		pagenum:10
	}
	laydate.render({
	  elem: '.time'
	  ,range: true,
	  done:function(value, date, endDate){
	  	arr = value.split(' - ')
	  	$("input[name='start_time']").val(arr[0])
	  	$("input[name='end_time']").val(arr[1])
	  }
	});
	var where_json={}
	{if $Think.get.car_type_name}
		where_json.car_type_name=['like','%{$Think.get.car_type_name}%']
	{/if}
	{if $Think.get.name}
		if($('.orderertype').val()==1){
			where_json.passenger_name=['like','%{$Think.get.name}%']
		}else if($('.orderertype').val()==2){
			where_json.orderer=['like','%{$Think.get.name}%']
		}
		
	{/if}
	{if $Think.get.passenger_phone}
		where_json.passenger_phone=['like','%{$Think.get.passenger_phone}%']
	{/if}
	{if $Think.get.start_time}
		where_json.orders_time=['between time',['{$Think.get.start_time}','{$Think.get.end_time}']]
	{/if}
	data.where_json=JSON.stringify(where_json)
	ajaxorder(data)
	function ajaxorder(data){
		Load()
		$.ajax({
			type:'post',
			data:data,
			url:'{:url("/adminapi/Order/orderList")}',
			success:function(res){
				Load(1)
				console.log(res)
				if(res.code==1){
				res=orderstate(res)
//				$('tbody').html(tbodymainf2(res.data, 1, tddata, addoperation, 'rapair_no'))
				console.log(res.data)
					layui.use('table', function(){
					  	var table = layui.table;
					  
					  //展示已知数据
					 	 table.render({
					    	elem: '#order'
						    ,cols: [[ //标题栏
						      {field: 'order_no', title: '订单号', width: 250, sort: true,fixed: 'left'}
						      ,{field: 'order_type2', title: '订单类型', width: 150}
						      ,{field: 'order_state2', title: '订单状态', minWidth: 150}
						      ,{field: 'orderer', title: '下单人', minWidth: 150}
						      ,{field: 'order_time', title: '下单时间', width: 180}
						      ,{field: 'passenger_name', title: '乘客', minWidth: 150}
						      ,{field: 'passenger_phone', title: '乘客电话', minWidth: 150}
						      ,{field: 'company_name', title: '乘车单位', width: 150}
						      ,{field: 'from_addr', title: '出发地', minWidth: 150}
						      ,{field: 'to_addr', title: '目的地', minWidth: 150}
						      ,{field: 'approval', title: '审批人', width: 150}
						      ,{field: 'driver_name', title: '司机', minWidth: 150}
						      ,{field: 'car_number', title: '车牌', minWidth: 150}
						      ,{field: 'car_type_name', title: '车型', width: 150}
						      ,{field: 'use_explain', title: '用车说明', minWidth: 150}
						      ,{field: 'passenger_money', title: '订单费用', minWidth: 150}
						      ,{field: 'meter', title: '订单里程', minWidth: 150}
						      ,{field: 'use_time', title: '用时', minWidth: 180}
						      ,{field: 'apply_time', title: '申请时间', width: 180}
						      ,{field: 'approval_time', title: '批准时间', width: 180}
						      ,{field: 'orders_time', title: '接单时间', width: 180}
						      ,{field: 'ride_time', title: '乘车时间', width: 180, sort: true}
						    ]]
						    ,data:res.data
						    //,skin: 'line' //表格风格
						    ,even: true
						    //,page: true //是否显示分页
						    //,limits: [5, 7, 10]
						    //,limit: 5 //每页默认显示的数量
					  	});
					  	$('.pagination').html(pagechang1(res.count, 10, page))
					  	$('.laquo').on('click',function(){
							console.log(data)
							page-=1;
							data.page=page
							ajaxorder(data)
						})
						$('.pageli').on('click',function(){
							console.log(data)
							page=$(this).html()
							data.page=page
							ajaxorder(data)
						})
						//下一页
						$('.raquo').on('click',function(){
							console.log(data)
							page+=1;
							data.page=page
							ajaxorder(data)
						});
						
					});
				
				
				$('.daochu').on('click',function(){
					var getkey="";
//					for(var key in res.description){
//						getkey+=`<li>
//									<label class="am-checkbox">
//								      <input type="checkbox" value="" data-key='${key}' checked> ${res.description[key].name}
//								    </label>
//								</li>`
//					}
					

					for(var iii=0;iii<tddata.length;iii++){
						if(tddata[iii].key=='order_type2'){
							for(var key in res.description){
								if(key=='order_type'){
									tddata[iii].text=res.description[key].name
								}
							}
							getkey+=`<li>
									<label class="am-checkbox">
								      <input type="checkbox" value="" data-key='order_type' checked> ${tddata[iii].text}
								    </label>
								</li>`
						}else if(tddata[iii].key=='order_state2'){
							for(var key in res.description){
								if(key=='order_state'){
									tddata[iii].text=res.description[key].name
								}
							}
							getkey+=`<li>
									<label class="am-checkbox">
								      <input type="checkbox" value="" data-key='order_state' checked> ${tddata[iii].text}
								    </label>
								</li>`
						}else{
							for(var key in res.description){
								if(key==tddata[iii].key){
									tddata[iii].text=res.description[key].name
								}
							}
							getkey+=`<li>
									<label class="am-checkbox">
								      <input type="checkbox" value="" data-key='${tddata[iii].key}' checked> ${tddata[iii].text}
								    </label>
								</li>`
						}
						
					}
					layer.open({
					  	type: 1,
					  	title:false,
					  	skin: 'layui-layer-rim', //加上边框
					  	area: ['300px', '400px'], //宽高
					  	content: `<div>
					 				<div style='height:300px;overflow:auto;margin-bottom:30px'>
								  	<ul class='daochuul'>${getkey}</ul>
								  	</div>
								  	<div style='text-align:center'>
								  		<button class='am-btn am-btn-sm am-btn-primary am-radius daochu_ok' type='button'>确定导出</button>
								  		<button class='am-btn am-btn-sm am-btn-primary am-radius' type='button'>取消</button>
								  	</div>
								</div>`,
						success:function(){
							var ddata={}
							$('.daochu_ok').on('click',function(){
//								window.open('{:url("/adminapi/Order/downLoadOrderList")}')
								ddata.fields="";
								var where_jsondao={};
								{if $Think.get.car_type_name}
									where_jsondao.car_type_name=['like','%{$Think.get.car_type_name}%']
								{/if}
								{if $Think.get.name}
									if($('.orderertype').val()==1){
										where_jsondao.passenger_name=['like','%{$Think.get.name}%']
									}else if($('.orderertype').val()==2){
										where_jsondao.orderer=['like','%{$Think.get.name}%']
									}
									
								{/if}
								{if $Think.get.passenger_phone}
									where_jsondao.passenger_phone=['like','%{$Think.get.passenger_phone}%']
								{/if}
								{if $Think.get.start_time}
									where_jsondao.orders_time=['between time',['{$Think.get.start_time}','{$Think.get.end_time}']]
								{/if}
								ddata.where_json=JSON.stringify(where_jsondao)
								
								$('.daochuul li input[type="checkbox"]').each(function(){
									if(this.checked){
										ddata.fields+=($(this).data('key')+",")
									}
								})
								ddata.fields=ddata.fields.slice(0,ddata.fields.length-1)
								console.log(ddata.fields)

								let encodeWhere = encodeURI(ddata.where_json)
								window.open(`{:url("/adminapi/Order/downLoadOrderList")}?where_json=${encodeWhere}&fields=${ddata.fields}`)
//								
							})
							
						}
					});
				})
			}
			},
			error:function(){
				Load(1)
				Err('请求失败')
			}
		})
	}
})
$(function(){

})
